<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2019/1/12
 * Time: 0:24
 */

namespace api\storage\service\inventory;

use api\storage\common\Constant;
use api\storage\model\PurchaseDetailModel;
use api\storage\service\MaterielService;
use service\file\CommonFile;

/**
 * 物料盘点基类
 *
 * Class BaseMaterielInventoryService
 * @package api\storage\service\inventory
 */
abstract class BaseMaterielInventoryService extends BaseInventoryService
{
    protected $basePath;

    public function __construct()
    {
        parent::__construct();
        $this->basePath = Constant::RECORD_INVENTORY . 'base' . Constant::RECORD_FILE_SUFFIX;
    }

    /**
     * 是否有灯
     *
     * @return mixed
     */
    abstract protected function isWhite();

    /**
     * 物料实现类的参数检查
     *
     * @param $code
     * @param $storageUuid
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function afterCheck($code, $storageUuid)
    {
        //判断code参数是reelId 还是subMaterielId
        $materielData = MaterielService::getMaterielCache($code);
        if ($this->isEmpty($materielData)) {
            $purchaseDetailModel = new PurchaseDetailModel();
            $materielId = $purchaseDetailModel->findMaterielIdByReelId($code);
            if ($this->isEmpty($materielId)) {
                return $this->resultTemplate(-32);
            }
        } else {
            $materielId = $materielData['sub_materiel_id'];
        }
        $file = new CommonFile($storageUuid, $this->basePath);
        $content = $this->jsonDecode($file->read());
        if (!empty($content)) {
            $materielList = array_filter(array_column($content, 'materielId'));
            $kind = count($materielList);
            //物料不在物料列表里 并且 种类大于3 并且是有灯的仓库
            $flag = !in_array($materielId, $materielList) && $kind >= 3 && $this->isWhite();
            if ($flag) {
                $this->logger->debug("根据物料：{$materielId}盘点，物料种类超过三种");
                return $this->resultTemplate(-31);
            }
        }
        return $this->resultTemplate(1, ['value' => $materielId]);
    }


    protected function handleReels($reels)
    {
        $reelsInfo = [];
        $header = [];
        $shelfInfo = [];
        $containerInfo = [];
        $containerCode = array_values(array_unique(array_filter(array_column($reels['reels'], 'containerCode'))));
        foreach ($reels['reels'] as $k => $item) {
            $item['isFinish'] = false;
            $item['outCount'] = $item['count'];
            $reelsInfo[] = $item;
            if (!empty($item['containerCode'])) {
                $key = array_search($item['containerCode'], $containerCode);
                $containerInfo[$key]['containerCode'] = $item['containerCode'];
                $containerInfo[$key]['seatCode'] = $item['seatCode'];
                $containerInfo[$key]['plate'] = isset($containerInfo[$key]['plate']) ? $containerInfo[$key]['plate']++ : 1;
                $containerInfo[$key]['confirm'] = 0;
            } else {
                $head = substr($item['seatCode'], 0, 3);
                $key = array_search($head, array_column($shelfInfo, 'shelvesCode'));
                if ($key !== false) {
                    $shelfInfo[$key]['plate'] = isset($shelfInfo[$key]['plate']) ? $shelfInfo[$key]['plate']++ : 1;
                } else {
                    array_push($shelfInfo, ['shelvesCode' => $head, 'plate' => 1, 'confirm' => 0]);
                }
            }
        }
        foreach ($shelfInfo as $item) {
            $header[] = $item;
        }
        foreach ($containerInfo as $item) {
            $header[] = $item;
        }
        return $ret = [
            'reels' => $reelsInfo,
            'header' => $header,
            'seatId' => $reels['seatId']
        ];
    }
}